﻿using BigEgg.Framework.Foundation;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace FMStudio.Domain.SQLite
{
    [MetadataType(typeof(IBill))]
    public partial class Bill : IBill, IDataErrorInfo
    {
        #region Implement IDataErrorInfo Interface
        [NonSerialized]
        private readonly DataErrorInfoSupport dataErrorInfoSupport;

        string IDataErrorInfo.Error { get { return this.dataErrorInfoSupport.Error; } }

        string IDataErrorInfo.this[string memberName] { get { return this.dataErrorInfoSupport[memberName]; } }
        #endregion


        public Bill()
            : this(0)
        {
        }

        public Bill(int index)
        {
            this.dataErrorInfoSupport = new DataErrorInfoSupport(this);

            //  Default value
            this.Date = DateTime.Today;
            this.PayTypeString = string.Empty;
            this.Index = index;
            this.Out = true;
            this.Number = 1.0;
            this.Discount = 100.0;

            this.UseTypeReference.AssociationChanged += UseTypeReferenceAssociationChanged;
            this.ItemPriceReference.AssociationChanged += ItemPriceReferenceAssociationChanged;
        }


        #region Implement IBill Interface
        IUseType IBill.UseType
        {
            get { return this.UseType; }
            set { this.UseType = value as UseType; }
        }

        IItemPrice IBill.ItemPrice
        {
            get { return this.ItemPrice; }
            set { this.ItemPrice = value as ItemPrice; }
        }
        #endregion


        #region Private Methods
        private void UseTypeReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("UseType");
            this.UseTypeID = this.UseType.ID;
        }

        private void ItemPriceReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("ItemPrice");
            this.ItemPriceID = this.ItemPrice.ID;
        }
        #endregion
    }
}
